/**
 * @description 购物车
 * @author 赵子靖
 * @since 2017-12-15
 */
var Cart = function() {
    var t = {
        path :'',
        init : function() {
            t.initView();
            t.initEvent();
        },
        initView : function() {
            
        },
        initEvent : function() {
            
        },
        //加
        plusNum:function(shopId){
            var shopNum = $("#"+shopId+"-num").html();
            shopNum = shopNum * 1 + 1;
            $("#"+shopId+"-num").html(shopNum);
        },
        //减
        minusNum:function(shopId){
            var shopNum = $("#"+shopId+"-num").html();
            if(shopNum>0){
                shopNum = shopNum * 1 - 1;
                $("#"+shopId+"-num").html(shopNum);
            }
        },
        //加入购物篮
        shopAddCard:function(shopId){
            
            var shopNum = $("#"+shopId+"-num").html();
            if(shopNum<=0){
                layer.open({
                    content: '您还没有选择购买数量'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }else{
                //加入购物车操作
                layer.open({
                    type: 2
                    ,content: '处理中'
                });
                
                $.ajax({
                    type:"POST",
                    url:t.path+"/mobile/shopCart/addShopCart.do",
                    dataType:"json",
                    data:{
                        shopId:shopId,
                        shopNum:shopNum
                    },
                    async: false,
                    cache:false,
                    success:function(data){
                        layer.closeAll();
                        if(data.success){
                            //添加成功
                            layer.open({
                                content: data.msg
                                ,skin: 'msg'
                                ,time: 2 //2秒后自动关闭
                            });
                            //刷新购物车数量
                            t.getCartNum();
                        }else{
                            //添加失败
                            layer.open({
                                title: [
                                  '操作失败',
                                  'background-color: #FF4351; color:#fff;'
                                ]
                                ,content: data.msg
                            });
                        }
                    }
                });
            }
        },
        //刷新数量
        getCartNum:function(){
            $.ajax({
                type:"POST",
                url:t.path+"/mobile/shopCart/getCartNum.do",
                dataType:"json",
                data:{
                },
                async: false,
                cache:false,
                success:function(data){
                    $("#shopCartNum").html(data.total);
                }
            });
        },
        //加并计算金额
        plusNumCalcMoney:function(shopId){
            var shopNum = $("#"+shopId+"-num").html();
            shopNum = shopNum * 1 + 1;
            t.commonAddShopToCard(shopId,+1);
        },
        //减并计算金额
        minusNumCalcMoney:function(shopId){
            var shopNum = $("#"+shopId+"-num").html();
            if(shopNum>0){
                shopNum = shopNum * 1 - 1;
                t.commonAddShopToCard(shopId,-1);
            }
            
        },
        //添加进入购物车通用方法
        commonAddShopToCard:function(shopId,shopNum){
            //加入购物车操作
            layer.open({
                type: 2
                ,content: '处理中'
            });
            
            $.ajax({
                type:"POST",
                url:t.path+"/mobile/shopCart/updateShopCart.do",
                dataType:"json",
                data:{
                    shopId:shopId,
                    shopNum:shopNum
                },
                async: false,
                cache:false,
                success:function(data){
                    layer.closeAll();
                    if(data.success){
                        //添加成功
                        layer.open({
                            content: data.msg
                            ,skin: 'msg'
                            ,time: 2 //2秒后自动关闭
                        });
                        //刷新购物车数量
                        window.location.reload();
                    }else{
                        //添加失败
                        layer.open({
                            title: [
                              '操作失败',
                              'background-color: #FF4351; color:#fff;'
                            ]
                            ,content: data.msg
                        });
                        window.location.reload();
                    }
                }
            });
        },
        //清楚购物车
        clearAllCart:function(){
            layer.open({
                content: '您确定要清空购物车吗？'
                ,btn: ['确定', '取消']
                ,yes: function(index){
                    $.ajax({
                        type:"POST",
                        url:t.path+"/mobile/shopCart/deleteAllShopCart.do",
                        dataType:"json",
                        data:{
                        },
                        async: false,
                        cache:false,
                        success:function(data){
                            layer.close(index);
                            if(data.success){
                                //删除成功
                                layer.open({
                                    content: data.msg
                                    ,skin: 'msg'
                                    ,time: 2 //2秒后自动关闭
                                });
                                window.location.reload();
                            }else{
                                //添加失败
                                layer.open({
                                    title: [
                                      '操作失败',
                                      'background-color: #FF4351; color:#fff;'
                                    ]
                                    ,content: data.msg
                                });
                            }
                        }
                    });
                }
            });
        },
        //立即结算
        forwardSubmitOrder:function(){
            layer.open({
                type: 2
                ,content: '处理中'
            });
            window.location.href=t.path+"/mobile/shopCart/forwardSubmitOrder.do";
        },
        //直接购买商品
        shopBuy:function(shopId){
            layer.open({
                type: 2
                ,content: '处理中'
            });
            //购买数量
            var shopNum = $("#"+shopId+"-num").html();
            window.location.href=t.path+"/mobile/order/forwardBuyOneShop.do?shopId="+shopId+"&shopNum="+shopNum;
        }
    }
    return t;
}();